package com.canin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.canin.model.Member;

public class MemberDAO
{
	private Connection conn = null;

	public MemberDAO(Connection conn)
	{
		this.conn = conn;
	}

	public Member getMemberByUserPass(String username, String password)
			throws SQLException
	{
		System.out.println("getMemberByUserPass");
		
		Member member = null;

		String sql = "SELECT * FROM MEMBER WHERE USERNAME = ? "
				+ "AND PASSWORD = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, username);
		ps.setString(2, password);

		ResultSet rs = ps.executeQuery();

		if (rs.next())
		{
			member = new Member();

			member.setMemberId(rs.getInt("ID"));
			member.setFirst_Name(rs.getString("FIRST_NAME"));
			member.setLast_Name(rs.getString("LAST_NAME"));
			member.setEmail(rs.getString("email"));
			member.setTelephone(rs.getString("telephone"));
			member.setAddress_Line1(rs.getString("address_Line1"));
			member.setAddress_Line2(rs.getString("address_Line2"));
			member.setAddress_Line3(rs.getString("address_Line3"));
			member.setAddress_Line4(rs.getString("address_Line4"));
			member.setPost_Code(rs.getString("post_Code"));
			
			System.out.println("Member : " + rs.getString("FIRST_NAME"));
		}

		rs.close();
		ps.close();

		return member;
	}
	
	public boolean updateMember( Member member ) throws Exception
	{
		String sql = "UPDATE MEMBER SET first_name=?, Last_Name=?, email=?, telephone=?, " +
				"address_line1=?, address_line2=?, address_line3=?, address_line4=?, post_code=? " +
				"WHERE id=?";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, member.getFirst_Name());
		ps.setString(2, member.getLast_Name());
		ps.setString(3, member.getEmail());
		ps.setString(4, member.getTelephone());
		ps.setString(5, member.getAddress_Line1());
		ps.setString(6, member.getAddress_Line2());
		ps.setString(7, member.getAddress_Line3());
		ps.setString(8, member.getAddress_Line4());
		ps.setString(9, member.getPost_Code());
		ps.setInt(10, member.getMemberId());
		
		ps.executeUpdate();
		ps.close();
		
		return true;
	}

	public Member getMemberById(int id) throws Exception
	{
		Member member = new Member();

		String sql = "SELECT * FROM MEMBER WHERE ID=? ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);

		ResultSet rs = ps.executeQuery();

		if (rs.next())
		{
			member.setEmail( rs.getString("email") );
			member.setFirst_Name( rs.getString("first_name") );
			member.setLast_Name( rs.getString("last_name") );
			member.setTelephone(rs.getString("telephone") );
			member.setAddress_Line1(rs.getString("address_line1") );
			member.setAddress_Line2( rs.getString("address_line2") );
			member.setAddress_Line3( rs.getString("address_line3") );
			member.setAddress_Line4( rs.getString("address_line4") );
			member.setPost_Code(rs.getString("post_code") );
		}

		rs.close();
		ps.close();

		return member;
	}
	
}
